package c.x.jy.code;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class FileUtil {
	/**
	 * 写文�?
	 * 
	 * @param newStr 新内�?
	 * @throws IOException
	 */
	public static boolean writeTxtFile(String text, String fileName) {
		// 先读取原有文件内容，然后进行写入操作
		boolean flag = false;
		String filein = text + "\r\n";
		String temp = "";

		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader br = null;

		FileOutputStream fos = null;
		PrintWriter pw = null;
		try {
			// 文件路径
			File file = new File(fileName);
			if( file.isFile() && file.exists() ) {
				System.out.println(file.getName()+" 文件已经存在！");
				return false;
			}
			
				
				creatTxtFile(fileName);
			/*
			 * if (!(file.isFile() && file.exists())) { // 判断文件是否存在 creatTxtFile(fileName);
			 * }
			 */
			// 将文件读入输入流
			fis = new FileInputStream(file);
			isr = new InputStreamReader(fis);
			br = new BufferedReader(isr);
			StringBuffer buf = new StringBuffer();

			// 保存该文件原有的内容
			for (; (temp = br.readLine()) != null;) {
				buf = buf.append(temp);
				// System.getProperty("line.separator")
				// 行与行之间的分隔�? 相当于�?�\n�?
				buf = buf.append(System.getProperty("line.separator"));
			}
			buf.append(filein);

			fos = new FileOutputStream(file);
			pw = new PrintWriter(fos);
			pw.write(buf.toString().toCharArray());
			pw.flush();
			flag = true;
		} catch (IOException e1) {
			// TODO 自动生成 catch �?
			try {
				throw e1;
			} catch (IOException e) {
				// TODO 自动生成�? catch �?
				e.printStackTrace();
			}
		} finally {

			if (pw != null) {
				pw.close();
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (isr != null) {
				try {
					isr.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}
		return flag;
	}

	/**
	 * 创建文件
	 * 
	 * @throws IOException
	 */
	public static void creatTxtFile(String fileName) throws IOException {

		int i = fileName.lastIndexOf(File.separator);

		String name = fileName.substring(i + 1);
		String path = fileName.replace(name, "");
		File pathfile = new File(path);

		if (!pathfile.exists()) {

			pathfile.mkdirs();

		}
		File file = new File(fileName);
		if (file.isFile() && file.exists()) {
			System.out.println("删除：" + file.getName());
			file.delete();

		}
		new File(fileName).createNewFile();
		System.out.println(fileName + " 创建成功！");
	}

	public static void main(String[] args) throws IOException {

		String userDir = System.getProperty("user.dir");
		creatTxtFile(
				userDir + "\\src\\main\\java\\c1\\x\\jy\\modules\\sys\\service\\" + "SubjeccustomerTestService.java");
	}
}
